<?php
declare (strict_types=1);

namespace app\api\controller\v2;

use app\common\model\Course;

use app\common\model\Datum;
use app\common\model\Grade;
use app\common\model\Mold;
use app\common\model\Question;
use app\common\model\Record;


class Beta extends BaseController
{
    /**
     * 在线测评
     *
     * @return \think\Response
     */
    public function index()
    {
        return sucessMsg(200, '获取成功', [
            'grade' => $this->grade(),   //获取小学年级信息
            'junior' => $this->junior(),   //获取初中年级信息
            'course' => $this->course(),   //获取课程分类信息
            'mold' => $this->mold(),   //获取类型分类信息
            'book' => $this->book(),   //获取课本名称和图片信息
        ]);
    }


    /**
     * 小学
     *
     * @return \think\Response
     */
    public function grade()
    {
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 12);
        $data = Grade::where(['status' => 1, 'type' => 1])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => $data];
    }

    /**
     * 初中
     *
     * @return \think\Response
     */
    public function junior()
    {
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 12);
        $data = Grade::where(['status' => 1, 'type' => 2])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => $data];
    }

    /**
     * 课程
     *
     * @return \think\Response
     */
    public function course()
    {
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 12);
        $data = Course::where(['status' => 1])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => $data];

    }

    /**
     *  类型
     *
     * @return \think\Response
     */
    public function mold()
    {
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 12);
        $data = Mold::where(['status' => 1])
            ->field('id,title')
            ->order(['id' => 'asc'])
            ->select();
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => $data];
    }

    /**
     * 在线测评列表
     */
    public function book()
    {
        $page = input('page/d', 1); // 页数
        $pageSize = input('pageSize/d', 12);    //  每页显示条数
        $grade_id = input('grade_id/d', 0);     // 年级id
        $course_id = input('course_id/d', 0);   // 课程—类型id
        $mold_id = input('mold_id/d', 0);   // 类型id

        //学习中心列表
        $where['status'] = 1;
        // 如果年级id不为空，则查询年级题目
        if ($grade_id > 0) $where['grade_id'] = $grade_id;
        // 如果课程类型id不为空，则查询课程类型题目
        if ($course_id > 0) $where['course_id'] = $course_id;
        // 如果课程类型id不为空，则查询课程类型题目
        if ($mold_id > 0) $where['mold_id'] = $mold_id;
        $list = Question::where($where)
            ->with('grades')
            ->field('id,name,image,grade_id,course_id,mold_id')
            ->withAttr('image', function ($val, $data) {
                return strtolower(config("app.domain_url") . str_replace('\\', '/', $val));
            })
            ->order(['grade_id' => 'asc', 'id' => 'asc'])
            ->group('grade_id')
            ->page($page)
            ->select()->toArray();
        foreach ($list as $k => $v) {
            $a['id'] = $v['id'];
            $grade_total = Question::where($a)->count();
            $grade_work_total = Record::where($a)->where(['api_member_id' => $this->user_id])->count();
            if ($grade_total == 0 || $grade_work_total == 0) {
                $list[$k]['grade_work_total'] = 0;
            } else {
                $list[$k]['grade_work_total'] = round($grade_work_total / $grade_total * 100);
            }
        }
        return ['page' => $page, 'pageSize' => $pageSize, 'data' => ['list' => $list, 'total' => count($list)]];
    }
}

